package statisticAnalysis;

import DL.Map3DTo2D;
import DL.SPos;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwj on 2018/4/10.
 */
public class LRviewportMovement {

    final static String csvPath = "J:\\360dataset\\sensory\\orientation"; // csv路径

    public static File[] getCsvFileNames() {
        File f = new File(csvPath);
        if (!f.exists()) {
            System.out.println(csvPath + " not exists");
            return null;
        }

        File listFiles[] = f.listFiles();
        return listFiles;
    }

    public static int getGapPos(int xGap){
        // offset 0~10 10~30 30~60 60~90 90~120 大于120
        int x1=0;
        if(xGap>=0&&xGap<10) x1=0;
        else if(xGap>=10&&xGap<30) x1=1;
        else if(xGap>=30&&xGap<60) x1=2;
        else if(xGap>=60&&xGap<90) x1=3;
            //else if(xGap>=90&&xGap<120) x1=4;
        else if(xGap>=90) x1=5;
        return x1;
    }

    public static void main(String[] args) {
        File[] listFiles=getCsvFileNames();
        List<SPos> sPosList;
        int xGap=0,x1=0,x2=0;
        double predictOffset=0;
        int[] count5f=new int[6];
        int[] count1s=new int[6];
        int[] count2s=new int[6];
        int[] count4s=new int[6];
// offset 0~10 10~30 30~60 60~90 90~120 大于120
        int[][] movement66=new int[6][6];
        long allCount1s=0,allCount2s=0,allCount4s=0,allCount5f=0;
        long countSum1s=0,countSum2s=0,countSum4s=0,countSum5f=0;
        double alpha,beta;
        double threshold=0.5;
        for(File csv:listFiles) {
            //System.out.println("开始读取" + csv.getName());
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(csv));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            sPosList=new ArrayList<SPos>(2048);
            try {
                String line = br.readLine();
                while ((line = br.readLine()) != null)  //读取到的内容给line变量
                {
                    String[] infos=line.split(",");
                    double x=Double.parseDouble(infos[1]);
                    double y=Double.parseDouble(infos[2]);
                    double z=Double.parseDouble(infos[3]);
                    sPosList.add(Map3DTo2D.map3DTo2D(new SPos(x,y,z),360,180));
                }
                //System.out.println("读取完" + csv.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }

            double  newX;
            for(int start=0;start<sPosList.size();start++){

                LineRegression line = new LineRegression();
                if(start+10+30+30<sPosList.size()) {
                    for (int i = start; i < start + 10; i++) {
                        line.addDataPoint(sPosList.get(i));
                    }
                    double a1=line.getA1();
                    double a0=line.getA0();
                    if(line.getR()>threshold) {
                        for (int i = 10+ 30; i < 10 + 30 + 30; i++) {
                            newX = (i * a1 + a0);
                            while (newX > 360) newX -= 360;
                            while (newX < 0) newX += 360;
                            xGap = (int) Math.abs(newX - sPosList.get(start + i).x);
                            xGap = Math.abs(xGap > 180 ? 360 - xGap : xGap);
                            allCount5f += xGap;
                            // offset 0~10 10~30 30~60 60~90 90~120 大于120
                            x1 = getGapPos(xGap);
                            count5f[x1]++;
                        }
                        countSum5f += 30;
                    }
                }


                LineRegression lr1s = new LineRegression();
                if(start+30+30+30<sPosList.size()) {
                    for (int i = start; i < start + 30; i++) {
                        lr1s.addDataPoint(sPosList.get(i));
                    }
                    double a1=lr1s.getA1();
                    double a0=lr1s.getA0();
                    if(lr1s.getR()>threshold) {
                        for (int i = 30 + 30; i < 30 + 30 + 30; i++) {
                            newX = (i * a1 + a0);
                            while (newX > 360) newX -= 360;
                            while (newX < 0) newX += 360;
                            xGap = (int) Math.abs(newX - sPosList.get(start + i).x);
                            xGap = Math.abs(xGap > 180 ? 360 - xGap : xGap);
                            allCount1s += xGap;
                            // offset 0~10 10~30 30~60 60~90 90~120 大于120
                            x1 = getGapPos(xGap);
                            count1s[x1]++;
                        }
                        countSum1s += 30;
                    }
                }

                LineRegression lr2s = new LineRegression();
                if(start+60+30+30<sPosList.size()) {
                    for (int i = start; i < start + 60; i++) {
                        lr2s.addDataPoint(sPosList.get(i));
                    }
                    double a1=lr2s.getA1();
                    double a0=lr2s.getA0();
                    if(lr2s.getR()>threshold) {
                        for (int i = 60 + 30; i < 60 + 30 + 30; i++) {
                            newX = (i * a1 + a0);
                            while (newX > 360) newX -= 360;
                            while (newX < 0) newX += 360;
                            xGap = (int) Math.abs(newX - sPosList.get(start + i).x);
                            xGap = Math.abs(xGap > 180 ? 360 - xGap : xGap);
                            allCount2s += xGap;
                            // offset 0~10 10~30 30~60 60~90 90~120 大于120
                            x1 = getGapPos(xGap);
                            count2s[x1]++;
                        }
                        countSum2s += 30;
                    }
                }

                LineRegression lr4s = new LineRegression();
                if(start+120+30+30<sPosList.size()) {
                    for (int i = start; i < start + 120; i++) {
                        lr4s.addDataPoint(sPosList.get(i));
                    }
                    double a1=lr4s.getA1();
                    double a0=lr4s.getA0();
                    if(lr1s.getR()>threshold) {
                        for (int i = 120 + 30; i < 120 + 30 + 30; i++) {
                            newX = (i * a1 + a0);
                            while (newX > 360) newX -= 360;
                            while (newX < 0) newX += 360;
                            xGap = (int) Math.abs(newX - sPosList.get(start + i).x);
                            xGap = Math.abs(xGap > 180 ? 360 - xGap : xGap);
                            allCount4s += xGap;
                            // offset 0~10 10~30 30~60 60~90 90~120 大于120
                            x1 = getGapPos(xGap);
                            count4s[x1]++;
                        }
                        countSum4s += 30;
                    }
                }

            }


        }
        System.out.println("count5f ");
        for(int i=0;i<6;i++)
            System.out.println(count5f[i]);
        System.out.println("-----------");
        System.out.println("count1s ");
        for(int i=0;i<6;i++)
            System.out.println(count1s[i]);
        System.out.println("-----------");
        System.out.println("count2s ");
        for(int i=0;i<6;i++)
            System.out.println(count2s[i]);
        System.out.println("-----------");
        System.out.println("count4s ");
        for(int i=0;i<6;i++)
            System.out.println(count4s[i]);
        System.out.println("-----------");
        System.out.println((allCount5f+0.0)/countSum5f);
        System.out.println((allCount1s+0.0)/countSum1s);
        System.out.println((allCount2s+0.0)/countSum2s);
        System.out.println((allCount4s+0.0)/countSum4s);





    }


}
